package com.wlf.admin.system.controller;

import cloud.tianai.captcha.application.vo.CaptchaResponse;
import cloud.tianai.captcha.application.vo.ImageCaptchaVO;
import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
import cloud.tianai.captcha.common.response.ApiResponse;
import cloud.tianai.captcha.application.ImageCaptchaApplication;
import cloud.tianai.captcha.spring.plugins.secondary.SecondaryVerificationApplication;
import cloud.tianai.captcha.validator.common.model.dto.ImageCaptchaTrack;
import cn.dev33.satoken.stp.StpUtil;
import com.wlf.admin.common.dto.LayuiMiniDto;
import com.wlf.admin.common.util.AjaxBean;
import com.wlf.admin.system.domain.SysUser;
import com.wlf.admin.system.service.IndexService;
import com.wlf.admin.system.service.SysUserService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 功能描述: 一些无关业务的API
 *
 * @author wlf 2023/9/13 10:30
 */
@Slf4j
@RestController
public class IndexController {
    @Resource
    IndexService indexService;
    @Resource
    private SysUserService userService;
    @Resource
    private ImageCaptchaApplication imageCaptchaApplication;


    /**
     * 功能描述: 获取验证码
     *
     * @author wlf 2023/9/15 23:04
     */
    @RequestMapping("/captcha")
    public CaptchaResponse<ImageCaptchaVO> genCaptcha(@RequestParam(value = "type", required = false) String type) {
        if (StringUtils.isBlank(type)) {
            type = CaptchaTypeConstant.SLIDER;
        }
        if ("RANDOM".equals(type)) {
            int i = ThreadLocalRandom.current().nextInt(0, 4);
            if (i == 0) {
                type = CaptchaTypeConstant.SLIDER;
            } else if (i == 1) {
                type = CaptchaTypeConstant.CONCAT;
            } else if (i == 2) {
                type = CaptchaTypeConstant.ROTATE;
            } else {
                type = CaptchaTypeConstant.WORD_IMAGE_CLICK;
            }

        }
        return imageCaptchaApplication.generateCaptcha(type);
    }


    /**
     * 功能描述: 校验验证码
     *
     * @author wlf 2023/9/15 23:03
     */
    @PostMapping("/captchaCheck")
    public ApiResponse<?> checkCaptcha(@RequestBody DataDto data) {
        ApiResponse<?> response = imageCaptchaApplication.matching(data.getId(), data.getData());
        if (response.isSuccess()) {
            return ApiResponse.ofSuccess(Collections.singletonMap("id", data.getId()));
        }
        return response;
    }

    @Data
    public static class DataDto {
        private String id;
        private ImageCaptchaTrack data;
    }

    /**
     * 功能描述: 登录表单
     *
     * @author wlf 2023/9/13 10:30
     */
    @GetMapping("/login")
    ModelAndView loginHtml() {
        return new ModelAndView("/main/login");
    }


    /**
     * 功能描述: 登录操作
     *
     * @author wlf 2023/9/13 10:30
     */
    @PostMapping("/login")
    AjaxBean login(String uname, String passwd, boolean remember, String captchaId) {
        return indexService.login(
                uname,
                passwd,
                remember,
                captchaId);
    }

    /**
     * 功能描述: 退出登录
     *
     * @author wlf 2023/9/13 10:31
     */
    @GetMapping("/logout")
    AjaxBean logout() {
        StpUtil.logout();
        return AjaxBean.getOkMsg();
    }

    /**
     * 功能描述: 无权限页面
     *
     * @author wlf 2023/9/13 10:31
     */
    @GetMapping("/error/403")
    ModelAndView error403() {
        ModelAndView view = new ModelAndView("/error/403");
        view.addObject("error", StpUtil.getSession().getString("error"));
        return view;
    }


    /**
     * 主页
     */
    @GetMapping("/")
    ModelAndView indexHtml() {
        ModelAndView view = new ModelAndView("/index");
        view.addObject("loginUser", userService.getLoginUser());
        return view;
    }

    /**
     * 前端框架初始化
     */
    @GetMapping("/init")
    LayuiMiniDto init() {
        return indexService.init();
    }

    /**
     * 功能描述: 首页
     *
     * @author wlf 2023/9/13 10:31
     */
    @GetMapping("/main")
    ModelAndView mainHtml() {
        return new ModelAndView("/main/index");
    }

    /**
     * 功能描述: 用户详情页面
     *
     * @author wlf 2023/9/13 10:31
     */
    @GetMapping("/userSetting")
    ModelAndView userSetting() {
        SysUser sysUser = userService.getLoginUser();
        ModelAndView view = new ModelAndView("/main/userSetting");
        view.addObject("user", sysUser);
        return view;
    }

    /**
     * 功能描述: 修改密码页面
     *
     * @author wlf 2023/9/13 10:31
     */
    @GetMapping("/userPass")
    ModelAndView userPassHtml() {
        SysUser sysUser = userService.getLoginUser();
        ModelAndView view = new ModelAndView("/main/userPass");
        view.addObject("user", sysUser);
        return view;
    }

    /**
     * 功能描述: 修改密码操作
     *
     * @author wlf 2023/9/13 10:34
     */
    @PostMapping("/userPass")
    AjaxBean userPass(String pass1, String pass2, String pass3) {
        return indexService.userPass(pass1, pass2, pass3);
    }

    /**
     * 功能描述: 清理缓存成功
     *
     * @author wlf 2023/9/13 14:06
     */
    @GetMapping("/cleanCache")
    AjaxBean cleanCache() {
        indexService.cleanCache();
        return AjaxBean.getOkMsg("清理缓存成功").setCode(1);
    }
}
